package com.testmanager.testmananger.tools.promotionSign;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class StandardSignUtils {

    public static boolean checkSign(String appkey, String appSecret, String requestMethod, String body, String signParam) {
        String realSign = sign(appkey, appSecret, requestMethod, body);
        if (StringUtils.equals(realSign, signParam)) {
            return true;
        }
        return false;
    }

    public static String sign(String appKey, String appSecret,
                              String requestMethod, String body) {
        byte[] signBytes = new StringBuilder(requestMethod).append(":")
            .append(body).append(":")
            .append(appKey).append(":")
            .append(appSecret).append(":")
            .toString().getBytes();
        return HMACSHA256.sign(signBytes, appSecret.getBytes());
    }

    public static void main(String[] args) throws  Exception{

        String appSecret = "EEF7D4AE200A55A3";
        String method = "POST";
        String appid = "856e4bb05b4f4f8abfc385880e7d7386";
        Map<String, Object> applyInfo = new HashMap<>();
        /*applyInfo.put("callback", "http://www.yingying.com/tracking?pcid={pcid}&idfa={idfa}");
        applyInfo.put("idfa", "2C27377C-0E84-443A-BCCF-EBA04C40C52B2");*/
        applyInfo.put("channel", "pc284671101866344448");
        applyInfo.put("idfa", "2C27377C-0E84-443A-BCCF-EBA04C40C52C");
        applyInfo.put("point", "pp284835109613608960");
        String applyInfoStr = URLEncoder.encode(Codec.strEncodBase64(appSecret, JSONObject.toJSONString(applyInfo)),"UTF-8");

        String sign = StandardSignUtils.sign(appid, appSecret, method, applyInfoStr);
        System.out.println(applyInfoStr);
        System.out.println(sign);






    }



}
